﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

/// <summary>
/// Summary description for CMyCart
/// </summary>
public class CMyCart
{
    private List<CCartItemp> _items;

    public List<CCartItemp> Items
    {
        get { return _items; }
        
    }  
	public CMyCart()
	{
        _items = new List<CCartItemp>();
	}
    //Them 1 itemps vao gio hàng
    public void AddItem(CCartItemp item)
    {
        //CCartItemp existedItem = _items.SingleOrDefault(c => c.ProducID == item.Product);
        int flag = 0;
        for (int i = 0; i < _items.Count; i++)
        {
            if (_items[i].Product == item.Product)
            {
                flag = 1;
                _items[i].Quantity += 1;
                _items[i].TinhThanhTien();
                break;
            }
        }
        if (flag == 0)
            _items.Add(item);
        
    }
    //Cap Nhật so luong 1 itemp 
    public void UpdateItem(int product, int quantity)
    {
       //CCartItemp itemUpdate = _items.Single(c => c.ProductID);
        //itemUpdate.Quantity = quantity;
        for(int i = 0;i<_items.Count;i++)
        {
            if (_items[i].Product == product)
            {
                _items[i].Quantity = quantity;
                break;
            }
        }
    }
    ///Xoá  1 sp ra khoi gio hang
    public void RemoveItem(int index)
    {
        //CCartItemp  itemToDel = _items.Single(c =>c.Product == productID)
            //_items.Remove(itemToDel);
        //for (int i = 0; i < _items.Count; i++)
        //    if (_items[i].Product == productID)
                _items.RemoveAt(index);
    }
    public int GetNumberOfFood()
    {
        int total = 0;
        for (int i = 0; i < _items.Count; i++)
            total += _items[i].Quantity;
        return total;
    }
    public int GetNumberOfItems()
    {
        return _items.Count;
    }

    public static long TongTien(List<CCartItemp> cart)
    {
        long s = 0;
        foreach (CCartItemp item in cart)
        {
            s += item.ThanhTien;
        }
        return s;
    }
}
